package com.shouer.doc.ui.billlist.presenter;

import android.widget.Toast;

import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.billlist.response.QueryBillResponse;
import com.shouer.doc.ui.billlist.view.IBillListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ZXL on 2016/12/20.
 */

public class BillListPresenterCompl implements IBillListPresenter {

    private IBillListView billListView;


    private boolean isRefreshing = false;
    private boolean isLoadingMore = false;

    private int currentPageNo = 1;
    private int maxPageNo = 30;
    private int pageNum = 20;


    public BillListPresenterCompl(IBillListView billListView) {
        this.billListView = billListView;
        EventBus.getDefault().register(this);
    }

    @Override
    public boolean refresh() {
        if (isRefreshing || isLoadingMore) {
            return false;
        }
        queryNewOrd(1);
        return true;
    }

    @Override
    public boolean loadMore() {
        if (isRefreshing || isLoadingMore || (currentPageNo + 1) > maxPageNo) {
            return false;
        }
        queryNewOrd(currentPageNo + 1);
        return true;
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        billListView = null;
    }

    public void queryNewOrd(int pageNo) {

        Map<String, String> param = new HashMap<>();
        param.put("pageNo", String.valueOf(pageNo));
        param.put("pageNum", String.valueOf(pageNum));
        param.put("docId", billListView.getUser().getUserId());

        if (pageNo == 1) {
            isRefreshing = true;
        } else {
            isLoadingMore = true;
        }

        final GsonRequest<QueryBillResponse> request = new GsonRequest("queryBill", QueryBillResponse.class, new Response.Listener<QueryBillResponse>() {
            @Override
            public void onResponse(QueryBillResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                isRefreshing = false;
                isLoadingMore = false;
                if (error instanceof ParseError) {
                    billListView.loadBillDataFailed("返回数据格式错误 查询收入明细失败");
                } else {
                    billListView.loadBillDataFailed("网络异常 查询收入明细失败");
                }

            }
        });

        RequestUtil.sendRequest(billListView.getContext(), param, request);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryBillResponse response) {
        isRefreshing = false;
        isLoadingMore = false;
        if (response.getErrorCode() >= 0) {
            if (response.getPageNo() == 1) {
                currentPageNo = 1;
                billListView.refreshBillList(response.getBillList());
            } else {
                if (null != response.getBillList() && response.getBillList().size() > 0) {
                    currentPageNo++;
                }
                billListView.attachBillList(response.getBillList());
            }
        } else {
            billListView.loadBillDataFailed(response.getErrorMsg());
            Toast.makeText(billListView.getContext(), response.getErrorMsg(), Toast.LENGTH_SHORT).show();
        }
    }
}
